package com.mervin.patterns.singleton.type6;

/***************************************
 * @Auther: mervin
 * @Date: 2020-04-15 23:11
 * @Description: 懒汉式（双重检查）
 ***************************************/
public class LazySingleton {

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                System.out.println(Singleton.getInstance());
            }).start();
        }
    }
}

class Singleton {
    private static volatile Singleton instance;
    private Singleton(){}
    public static Singleton getInstance(){
        if(instance == null){
            synchronized (Singleton.class){
                if(instance == null){
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}
